<?php

namespace app\common\model;

use think\Model;

/**
 * 会员积分日志模型
 */
class ScoreLog Extends Model
{

    // 表名
    protected $name = 'user_score_log';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    protected $hidden = ['id', 'user_id'];
    // 追加属性
    protected $append = [
        'createtime_text'
    ];

    public static function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    /**
     * 获取用户积分日志
     * @param int $user_id
     * @param string $sort
     * @param string $order
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($user_id, $sort = 'id', $order = 'DESC')
    {
        $list = self::where(['user_id' => $user_id])->order($sort, $order)->paginate();

        return $list;
    }
}
